	import App from './App'
	import {
		createApp
	} from 'vue'
	import api from './api/index.js'
	import TabBar from "./components/TabBar/TabBar.vue"

	import * as Pinia from 'pinia';
	const app = createApp(App)
	/**返回*/
	app.config.globalProperties.$fh = () => {
		const pages = getCurrentPages() // 获取当前页面栈
		const pageStackLength = pages.length // 页面栈层数
		console.log('当前页面栈层数:', pageStackLength)
		if (pageStackLength > 1) {
			uni.navigateBack() // 返回上一页
		} else {
			uni.switchTab({
				url: '/pages/index/index'
			}) // 跳转到首页 Tab
		}
	}
	//动态图片加载
	app.config.globalProperties.$getImagePath = (name, type = 0) => {
		if (type == 1) {
			return `/static/tabBar/${name}`
		} else {
			return `/static/${name}`
		}
	}
	// 价格切割
	app.config.globalProperties.$splitPrice = (price, type = false) => {
		if (!price) return {
			integer: '0.',
			decimal: '00'
		}
		// console.log('type', type);
		const [integer, decimal = '00'] = price.toString().split('.')

		if (type) {
			return {
				integer: integer.replace(/\B(?=(\d{3})+(?!\d))/g, ','), // 添加千位分隔符
				decimal: decimal.padEnd(2, '0') // 确保小数部分有两位
			}
		} else {
			return {
				integer: integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',') + '.', // 添加千位分隔符
				decimal: decimal.padEnd(2, '0') // 确保小数部分有两位
			}
		}
	}
	// 复制
	app.config.globalProperties.$fz = (value) => {
		uni.setClipboardData({
			data: value,
			success: function() {
				uni.showToast({
					title: '已复制',
					duration: 2000
				})
			}
		})
	}
	// 打电话
	app.config.globalProperties.$callPhone = (value) => {
		uni.makePhoneCall({
			phoneNumber: value
		})
	}
	// 计算顶部栏高度
	app.config.globalProperties.$Height = () => {
		// const systemInfo = wx.getSystemInfoSync()
		const menuButtonInfo = wx.getMenuButtonBoundingClientRect()

		// let data = {
		// 	statusBarHeight: systemInfo.statusBarHeight, // 状态栏高度
		// 	navBarHeight: menuButtonInfo.bottom, // 导航栏总高度
		// 	// menuButtonRight: systemInfo.screenWidth - menuButtonInfo.right, // 胶囊右边距
		// 	menuButtonHeight: menuButtonInfo.height, // 胶囊高度（32px）
		// }
		// console.log('高度', data);

		// return uni.getWindowInfo().statusBarHeight * 2
		return menuButtonInfo.bottom
	}
	app.config.globalProperties.$Width = () => {
		// const systemInfo = wx.getSystemInfoSync()
		const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
		return menuButtonInfo.width
	}

	// 检查是否在微信环境
	let isWeChatEnv = () => {
		// 方法1：使用 uni.getSystemInfoSync()
		const systemInfo = uni.getSystemInfoSync();
		if (systemInfo.platform === 'devtools' || systemInfo.environment === 'wx') {
			return true;
		}

		// 方法2：使用 uni.getEnv()（如果支持）
		if (typeof uni.getEnv === 'function' && uni.getEnv() === 'MP-WEIXIN') {
			return true;
		}

		return false;
	}
	// 校验
	const validateFields = (fields) => {
		console.log('fields', fields);
		for (let item of fields) {
			if ((item.condition == undefined || item.condition) && item.value == '') {
				uni.showToast({
					title: item.message,
					icon: 'none'
				})
				return true
			}
		}
		return false
	}
	//注册公共组件
	app.component('TabBar', TabBar)

	// 注册公共方法
	app.provide('$http', api)
	
	app.provide('$validateFields', validateFields)
	// app.provide('$isWeChatEnv', isWeChatEnv)
	app.use(Pinia.createPinia());
	app.mount('#app')